Core Foundation 与 Cocoa类型转换

__bridge //不涉及引用计数,用于不赋值或者不作为返回值时

CFArrayGetcount((__bridge CFArrayRef) nsArray); 

__bridge_transfer //把 Core Foundation类型转换为 Cocoa 类型

//减少cfString的计数,把所有权交给ARC 
NSString *nsString = (__bridge_transfer id)cfString; 
或者
NSString *nsString = CFBridgingRelease(cfString);

__bridge_retained //把 Cocoa 类型转换为 Core Foundation类型

//增加nsString的计数,把所有权交给Core Foundation,PS:别忘了使用后CFRelease
CFStringRef cfString = (__bridge_retained CFTypeRef)nsString;  
或者
CFStringRef cfString = CFBridgingRetain(nsString); 

以上规则的前提是我们拥有这个对象的ownership,否则,我们最好别转移所有权。比如:

NSMutableDictionary *keyAnimationDict = (__bridge id)CFDictionaryGetValue(_dict, (__bridge void *)obj); 
坚持原创技术分享,您的支持将鼓励我继续创作!